package com.teaching.teachingsupport.mapper.teacherviewmapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.teachingsupport.common.pojo.dataobject.TeacherCourse;
import com.teaching.teachingsupport.common.pojo.dto.response.CourseResponse;
import com.teaching.teachingsupport.common.pojo.dto.response.TeachingClassResponse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TeacherCourseMapper extends BaseMapper<TeacherCourse> {
    @Select("SELECT DISTINCT c.* FROM course c " +
            "JOIN teacher_course tc ON c.course_id = tc.course_id " +
            "WHERE tc.teacher_id = #{teacherId}")
    List<CourseResponse> findByTeacherId(Long teacherId);

    @Select("SELECT EXISTS(SELECT 1 FROM teacher_course tc " +
            "JOIN teaching_class tcl ON tc.course_id = tcl.course_id " +
            "WHERE tc.teacher_id = #{teacherId} " +
            "AND tc.course_id = #{courseId} " +
            "AND tcl.class_id = #{teachingClassId})")
    boolean validateTeacherCourseClass(@Param("teacherId") Long teacherId, 
                                     @Param("courseId") Long courseId, 
                                     @Param("teachingClassId") Long teachingClassId);

    @Select("SELECT " +
            "c.course_id as courseId, " +
            "c.course_name as courseName, " +
            "c.credit as credit, " +
            "c.course_type as courseType " +
            "FROM course c " +
            "INNER JOIN teacher_course tc ON c.course_id = tc.course_id " +
            "WHERE tc.teacher_id = #{teacherId} " +
            "AND c.course_name LIKE CONCAT('%', #{courseName}, '%')")
    List<CourseResponse> findByTeacherIdAndCourseName(@Param("teacherId") Long teacherId, @Param("courseName") String courseName);

    @Select("SELECT " +
            "tc.class_id as teachingClassId, " +
            "tc.class_name as className, " +
            "tc.semester_id as semesterId, " +
            "tc.grade as grade " +
            "FROM teaching_class tc " +
            "WHERE tc.course_id = #{courseId} " +
            "AND tc.teacher_id = #{teacherId} " +
            "AND tc.class_name LIKE CONCAT('%', #{className}, '%')")
    List<TeachingClassResponse> findByTeacherIdAndCourseIdAndClassName(
            @Param("teacherId") Long teacherId, 
            @Param("courseId") Long courseId,
            @Param("className") String className);

    @Select("SELECT c.course_name FROM course c WHERE c.course_id = #{courseId}")
    String findCourseNameById(@Param("courseId") Long courseId);

    @Select("SELECT DISTINCT u.username FROM user u " +
            "INNER JOIN teacher t ON u.user_id = t.user_id " +
            "INNER JOIN teacher_course tc ON t.user_id = tc.teacher_id " +
            "WHERE tc.teacher_id = #{teacherId} " +
            "LIMIT 1")
    String findTeacherNameById(@Param("teacherId") Long teacherId);

    @Select("SELECT CONCAT(s.start_year, '-', s.end_year, ' ', s.term) " +
            "FROM semester s WHERE s.semester_id = #{semesterId}")
    String findSemesterNameById(@Param("semesterId") Long semesterId);

    List<CourseResponse> findByTeacherIdAndCourseId(@Param("teacherId") Long teacherId, @Param("courseId") Long courseId);
}
